const conf = {
  properties:{},
  data: {
    hasEmptyGrid: false,
    point:[]
  },
  methods:{
    getThisMonthDays(year, month) {
      return new Date(year, month, 0).getDate();
    },
    getFirstDayOfWeek(year, month) {
      return new Date(Date.UTC(year, month - 1, 1)).getDay();
    },
    calculateEmptyGrids(year, month) {
      const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
      let empytGrids = [];
      if (firstDayOfWeek > 0) {
        for (let i = 0; i < firstDayOfWeek; i++) {
          empytGrids.push(i);
        }
        this.setData({
          hasEmptyGrid: true,
          empytGrids
        });
      } else {
        this.setData({
          hasEmptyGrid: false,
          empytGrids: []
        });
      }
    },
    calculateDays(year, month) {
      let days = [];

      const thisMonthDays = this.getThisMonthDays(year, month);
      for (let i = 1; i <= thisMonthDays; i++) {
        var dt = {
          index:i,
          date: year + "-" +( month < 10 ? "0" + month : month) + "-" +(i < 10 ? "0" + i: i),
          show:false
        }
        days.push(dt);
      }

      this.setData({
        days
      });
      this.pointDate();
    },
    pointDate(point){
      // var point = ["2018-04-02", "2018-04-03", "2018-04-06", "2018-04-08"]
      if(!point||point.length==0)return;
      var dd = this.data.days;
      for (var i = 0, len = dd.length;i<len;++i){
        for(var j=0,plen =point.length;j<plen;j++){
          if (point[j] == dd[i].date){
            dd[i].show = true;
          }
        }
      }
        this.setData({
          days: dd  
        });
    },
    handleCalendar(e) {
      const handle = e.currentTarget.dataset.handle;
      const cur_year = this.data.cur_year;
      const cur_month = this.data.cur_month;
      if (handle === 'prev') {
        let newMonth = cur_month - 1;
        let newYear = cur_year;
        if (newMonth < 1) {
          newYear = cur_year - 1;
          newMonth = 12;
        }

        this.calculateDays(newYear, newMonth);
        this.calculateEmptyGrids(newYear, newMonth);

        this.setData({
          cur_year: newYear,
          cur_month: newMonth
        })
        var eventDetail = {
          year: newYear,
          month: newMonth
        } // detail对象，提供给事件监听函数
        var eventOption = {} // 触发事件的选项
        this.triggerEvent('changeday', eventDetail, eventOption)

      } else {
        let newMonth = cur_month + 1;
        let newYear = cur_year;
        if (newMonth > 12) {
          newYear = cur_year + 1;
          newMonth = 1;
        }

        this.calculateDays(newYear, newMonth);
        this.calculateEmptyGrids(newYear, newMonth);

        this.setData({
          cur_year: newYear,
          cur_month: newMonth
        })
        var eventDetail = {
          year: newYear,
          month: newMonth
        } // detail对象，提供给事件监听函数
        var eventOption = {} // 触发事件的选项
        this.triggerEvent('changeday', eventDetail, eventOption)
      }
    }
  },
  attached(options) {
    const date = new Date();
    const cur_year = date.getFullYear();
    const cur_month = date.getMonth() + 1;
    const cur_day = date.getDate();
    const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
    this.calculateEmptyGrids(cur_year, cur_month);
    this.calculateDays(cur_year, cur_month);
    this.setData({
      cur_year,
      cur_month,
      weeks_ch,
      cur_day
    })
  }
};

Component(conf);
